home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_430.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  1.0 KB  |  13 lines

  1. No.
  2.  
  3. The names of, precedence of, associativity of, and arity of operators is fixed by the language.  There is no '**' operator in C++, so you cannot create one for a class type.
  4.  
  5. If you doubt the wisdom of this approach, consider the following code:
  6.     x = y ** z;
  7. Looks like your power operator?  Nope.  z may be a ptr, so this is actually:
  8.     x = y * (*z);
  9. Lexical analysis groups characters into tokens at the lowest level of the compiler's operations, so adding new operators would present an implementation nightmare (not to mention the increased maintenance cost to read the code!). 
  10.  
  11. Besides, operator overloading is just syntactic sugar for function calls.  It does not add fundamental power to the language (although this particular syntactic sugar can be very sweet, it is not fundamentally necessary).  I suggest you overload 'pow(base,exponent)', for which a double precision version is provided by the ANSI-C <math.h> library.
  12.  
  13. By the way: operator^ looks like a good candidate for to-the-power-of, but it has neither the proper precedence nor associativity.